<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
//use Illuminate\Support\Facades\Schema;

class CreateVipTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('members', function (Blueprint $table) {
            $columns = Schema::getColumnListing('members');

            if (!in_array('is_vip', $columns)) {
                $table->tinyInteger('is_vip')->default(0)->comment('是否VIP');
            }
            if (!in_array('vip_end_time', $columns)) {
                $table->integer('vip_end_time')->nullable()->comment('会员到期时间');
            }
        });

        if (!Schema::hasTable('vip')) {
            Schema::create('vip', function (Blueprint $table) {
                $table->id();
                $table->integer('day')->comment('天数');
                $table->integer('price')->comment('价格 单位分');
                $table->string('desc')->nullable()->comment('描述');
                $table->timestamps();
                $table->comment = 'vip信息';
            });
        }

        if (!Schema::hasTable('member_vip_log')) {
            Schema::create('member_vip_log', function (Blueprint $table) {
                $table->id();
                $table->integer('member_id')->index()->comment('会员ID');
                $table->string('operate_type')->comment('操作类型');
                $table->string('content')->comment('内容');
                $table->timestamps();
                $table->comment = '会员vip记录';
            });
        }

        if (!Schema::hasTable('member_vip_order')) {
            Schema::create('member_vip_order', function (Blueprint $table) {
                $table->id();
                $table->integer('member_id')->index()->comment('会员ID');
                $table->string('mini_type', 50)->index()->comment('小程序类型');
                $table->string('order_sn')->unique()->comment('订单号');
                $table->string('pay_sn')->unique()->comment('支付单号');
                $table->integer('pay_amount')->default(0)->comment('支付金额 单位分');
                $table->integer('pay_time')->default(0)->comment('支付时间');
                $table->tinyInteger('status')->default(0)->comment('支付状态 0-未支付 1-已支付 2-已退款');
                $table->integer('vip_id')->default(0)->comment('vip ID');
                $table->integer('vip_price')->default(0)->comment('vip价格');
                $table->integer('vip_day')->default(0)->comment('vip天数');
                $table->integer('start_time')->default(0)->comment('vip开始时间');
                $table->integer('end_time')->default(0)->comment('vip到期时间');
                $table->string('remark')->default('')->comment('备注');
                $table->timestamps();
                $table->comment = 'vip支付订单';
            });
        }

        if (!Schema::hasTable('pay_log')) {
            Schema::create('pay_log', function (Blueprint $table) {
                $table->id();
                $table->string('mini_type', 50)->index()->comment('小程序类型');
                $table->string('type', 50)->index()->comment('类型 vip-vip支付');
                $table->string('pay_sn')->comment('支付单号');
                $table->integer('pay_amount')->comment('支付金额 单位分');
                $table->string('pay_type')->comment('支付方式');
                $table->string('trade_no')->nullable()->comment('外部单号');
                $table->tinyInteger('is_pay')->default(0)->comment('是否支付 1-是 0-否');
                $table->integer('pay_time')->default(0)->comment('支付时间');
                $table->integer('notify_time')->default(0)->comment('回调时间');
                $table->text('extend')->nullable()->comment('扩展数据');
                $table->timestamps();
                $table->comment = '支付记录';
            });
        }

        if (!Schema::hasTable('refund_log')) {
            Schema::create('refund_log', function (Blueprint $table) {
                $table->id();
                $table->string('mini_type', 50)->index()->comment('小程序类型');
                $table->string('type', 50)->index()->comment('类型 vip-vip支付');
                $table->integer('order_id')->index()->comment('订单ID');
                $table->string('order_sn')->comment('订单号');
                $table->string('pay_sn')->comment('支付单号');
                $table->integer('pay_amount')->comment('支付金额 单位分');
                $table->string('pay_type')->comment('支付方式');
                $table->string('pay_trade_no')->nullable()->comment('外部订单号');
                $table->integer('refund_amount')->comment('退款金额');
                $table->string('refund_no')->unique()->comment('退款单号');
                $table->string('refund_trade_no')->nullable()->comment('外部退款单号');
                $table->tinyInteger('refund_status')->default(0)->comment('退款状态 0-退款中 1-退款成功 2-退款失败');
                $table->text('failed_msg')->nullable()->comment('失败原因');
                $table->timestamps();
                $table->comment = '退款记录';
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('members', function (Blueprint $table) {
            $columns = Schema::getColumnListing('members');
            if (in_array('is_vip', $columns)){
                $table->dropColumn('is_vip');
            }
            if (in_array('vip_end_time', $columns)){
                $table->dropColumn('vip_end_time');
            }
        });
        Schema::dropIfExists('vip');
        Schema::dropIfExists('member_vip_log');
        Schema::dropIfExists('member_vip_order');
        Schema::dropIfExists('pay_log');
    }
}
